summaryrefslogtreecommitdiffstats
path: root/src/core/hle/kernel/k_port.cpp
blob: 0a45ffd57bd6f6dfa7391f19a013e0614f080455 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "core/hle/kernel/k_port.h"
#include "core/hle/kernel/k_scheduler.h"
#include "core/hle/kernel/svc_results.h"

namespace Kernel {

KPort::KPort(KernelCore& kernel_)
    : KAutoObjectWithSlabHeapAndContainer{kernel_}, server{kernel_}, client{kernel_} {}

KPort::~KPort() = default;

void KPort::Initialize(s32 max_sessions_, bool is_light_, const std::string& name_) {
    // Open a new reference count to the initialized port.
    Open();

    // Create and initialize our server/client pair.
    KAutoObject::Create(std::addressof(server));
    KAutoObject::Create(std::addressof(client));
    server.Initialize(this, name_ + ":Server");
    client.Initialize(this, max_sessions_, name_ + ":Client");

    // Set our member variables.
    is_light = is_light_;
    name = name_;
    state = State::Normal;
}

void KPort::OnClientClosed() {
    KScopedSchedulerLock sl{kernel};

    if (state == State::Normal) {
        state = State::ClientClosed;
    }
}

void KPort::OnServerClosed() {
    KScopedSchedulerLock sl{kernel};

    if (state == State::Normal) {
        state = State::ServerClosed;
    }
}

bool KPort::IsServerClosed() const {
    KScopedSchedulerLock sl{kernel};
    return state == State::ServerClosed;
}

Result KPort::EnqueueSession(KServerSession* session) {
    KScopedSchedulerLock sl{kernel};

    R_UNLESS(state == State::Normal, ResultPortClosed);

    server.EnqueueSession(session);

    return ResultSuccess;
}

} // namespace Kernel